package com.gem.talkbase.adapter;


import java.util.List;

import net.tsz.afinal.FinalBitmap;
import net.tsz.afinal.FinalHttp;
import net.tsz.afinal.http.AjaxCallBack;
import net.tsz.afinal.http.AjaxParams;
import com.gem.talkbase.R;
import com.gem.talkbase.activity.AddFirendActivity;
import com.gem.talkbase.activity.MainActivity;
import com.gem.talkbase.bean.FirendMsg;

import com.gem.talkbase.client.Client;
import com.gem.talkbase.fragment.Fragment_message;

import android.R.integer;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;


public class RecentChatAdapter extends BaseAdapter {
	private List<FirendMsg> list2;
	private List<FirendMsg> list1;
	private Context cxt;
	private String name,date,msg,userid,unick,image;
	private String requestPath=Client.Firend_unick_image;
	FinalBitmap fb ;
	LayoutInflater inflater;
	private View context;
	Fragment_message fragment_message;
	private Context context2;
	String deletefirendmsgPath=Client.Delete_Firend_Msg;
	public RecentChatAdapter(Context context2,List<FirendMsg> list,String id){
		 this.context2=context2;
		 this.list1=list;
		 System.out.println(context2+"====================context2");
		 inflater = LayoutInflater.from(context2);	 
		  System.out.println("========s21312312312312312312==========");
		  this.userid=id;
		  
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
	System.out.println("hjasdashdhashdhashdhashdhsdhashdhhasdhhas========================");//
		final ViewHolder viewHolder;
		if(convertView==null){
		convertView = inflater.inflate(R.layout.recent_listview_item,null);
		}
		  fb = FinalBitmap.create(context2);
		viewHolder = new ViewHolder();
		System.out.println(convertView+"=================================jjjjjj");
		viewHolder.jidView = (TextView) convertView.findViewById(R.id.recent_list_item_name);//名字
		viewHolder.imageView = (ImageView) convertView.findViewById(R.id.icon);//头像
		viewHolder.dataView = (TextView) convertView.findViewById(R.id.recent_list_item_time);//时间
		viewHolder.msgView = (TextView) convertView.findViewById(R.id.recent_list_item_msg);//消息
	//	holder.unReadView = (TextView) convertView.findViewById(R.id.unreadmsg);
		System.out.println("shaolei==========================================");
//		if(parent.getChildCount() == position)
//		{
		FirendMsg entity = list1.get(position);
		name=entity.getFirendid();
		date=entity.getDate();
		msg=entity.getMsg();
		image=entity.getUimage();
		unick=entity.getUnick();
		System.out.println(date+"==============>"+msg+"===================>"+image+"=======>"+unick);
	//	viewHolder = buildHolder(convertView, name);
		 System.out.println("===============================================");
       
     
        System.out.println(userid+"==============================================="+unick);
     
          viewHolder.jidView.setText(unick);//设置名字
		  viewHolder.msgView.setText(msg);//设置消息
		  viewHolder.dataView.setText(date);//设置时间
		  fb.display(viewHolder.imageView,image);	  
		  viewHolder.deleteBtn = (Button) convertView.findViewById(R.id.recent_del_btn);//删除
		  viewHolder.deleteBtn.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					fun(position);	//点击listView的item去数据库删除相应的一条记录			
				}
		 });
  
		return convertView;
		
	}

	public void fun(final int positon){
		AjaxParams params = new AjaxParams();
		params.put("userid", userid);
		params.put("firendid", name);
		System.out.println(userid+"======"+name);
		FinalHttp fh = new FinalHttp();
		fh.post(deletefirendmsgPath, params, new AjaxCallBack<String>() {
			@Override
			public void onFailure(Throwable t, String strMsg) {
				System.out.println("加载失败！！！");
				super.onFailure(t, strMsg);
			}
			@Override
			public void onLoading(long count, long current) {
				// TODO Auto-generated method stub
				System.out.println("正在加载！！！");
				super.onLoading(count, current);
			}
			
			@Override
			public void onSuccess(String jsonString) {
	 			super.onSuccess(jsonString); 
	 		
	 			 list1.remove(positon);//点击哪条listView删除哪条
			   RecentChatAdapter.this.notifyDataSetInvalidated();
	 	      //  System.out.println("===========>>>>"+firendnuick+"-----"+firendmsg+"------"+firenddate);
			  
	      }
	});
  
}  
	 
	
//	private ViewHolder buildHolder(View convertView, final String jid) {
//		ViewHolder holder = new ViewHolder();
//		System.out.println(convertView+"=================================jjjjjj");
//		holder.jidView = (TextView) convertView.findViewById(R.id.recent_list_item_name);//名字
//		holder.imageView = (ImageView) convertView.findViewById(R.id.icon);//头像
//		holder.dataView = (TextView) convertView.findViewById(R.id.recent_list_item_time);//时间
//		holder.msgView = (TextView) convertView.findViewById(R.id.recent_list_item_msg);//消息
//	//	holder.unReadView = (TextView) convertView.findViewById(R.id.unreadmsg);
//		holder.deleteBtn = (Button) convertView.findViewById(R.id.recent_del_btn);//删除
//		holder.deleteBtn.setOnClickListener(new OnClickListener() {
//			@Override
//			public void onClick(View v) {
////				removeChatHistoryDialog(jid, XMPPHelper.splitJidAndServer(jid));
//			}
//		});
//		return holder;
//	}

	private static class ViewHolder {
	    ImageView imageView;//头像
		TextView jidView;//名字
		TextView dataView;//时间
		TextView msgView;//消息
//		TextView unReadView;//消息数量提示
		Button deleteBtn;//删除个人消息item
	}

	@Override
	public int getCount() {//getCount()为0则不会执行getView()方法。
		// TODO Auto-generated method stub
			return list1.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return list1.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

//	void removeChatHistory(final String JID) {
//		mContentResolver.delete(ChatProvider.CONTENT_URI,
//				ChatProvider.ChatConstants.JID + " = ?", new String[] { JID });
//	}
//
//	void removeChatHistoryDialog(final String JID, final String userName) {
//		new CustomDialog.Builder(mContext)
//				.setTitle(R.string.deleteChatHistory_title)
//				.setMessage(
//						mContext.getString(R.string.deleteChatHistory_text,
//								userName, JID))
//				.setPositiveButton(android.R.string.yes,
//						new DialogInterface.OnClickListener() {
//							public void onClick(DialogInterface dialog,
//									int which) {
//								removeChatHistory(JID);
//							}
//						})
//				.setNegativeButton(android.R.string.no,
//						new DialogInterface.OnClickListener() {
//
//							@Override
//							public void onClick(DialogInterface dialog,
//									int which) {
//							}
//						}).create().show();
//	}
}
